/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2010
Version 0.1
=======================================================*/
#include "database/DataBase.h"
#include "httpd/SAccount.h"
#include "ACSConfig.h"

namespace acs
{

std::vector<std::string> SAccount::getMenuHtml(std::string divID, int menuIndex)
{
	std::stringstream stream;
	std::vector<std::string> result;

	stream << ("<div style=\"float:left;\">");

	stream << ("<a href=\"oldindex.html\" class=\"mainMenu\"><img src=\"images/img_topo.gif\"/></a>");
	switch(menuIndex)
	{
		case MENU_INDEX:
		{
			stream << ("<a href=\"oldindex.html\" class=\"mainMenuSelected\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Home&nbsp;&nbsp;&nbsp;</span></a>");
			stream << ("<a href=\"devices.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Devices&nbsp;&nbsp;&nbsp;</span></a>");
//			stream << ("<a href=\"groups.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Groups&nbsp;&nbsp;&nbsp;</span></a>");
			stream << ("<a href=\"configurations.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Configurations&nbsp;&nbsp;&nbsp;</span></a>");
			stream << ("<a href=\"javascript: $('loading').style.visibility='visible'; window.location='/logout?url=login.html'\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Logout&nbsp;&nbsp;&nbsp;</span></a>");
		}break;

		case MENU_DEVICES:
		{
			stream << ("<a href=\"oldindex.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Home&nbsp;&nbsp;&nbsp;</span></a>");
			stream << ("<a href=\"devices.html\" class=\"mainMenuSelected\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Devices&nbsp;&nbsp;&nbsp;</span></a>");
//			stream << ("<a href=\"groups.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Groups&nbsp;&nbsp;&nbsp;</span></a>");
			stream << ("<a href=\"configurations.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Configurations&nbsp;&nbsp;&nbsp;</span></a>");
			stream << ("<a href=\"javascript: $('loading').style.visibility='visible'; window.location='/logout?url=login.html'\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Logout&nbsp;&nbsp;&nbsp;</span></a>");
		}break;

//		case MENU_GROUPS:
//		{
//			stream << ("<a href=\"oldindex.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Home&nbsp;&nbsp;&nbsp;</span></a>");
//			stream << ("<a href=\"devices.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Devices&nbsp;&nbsp;&nbsp;</span></a>");
//			stream << ("<a href=\"groups.html\" class=\"mainMenuSelected\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Groups&nbsp;&nbsp;&nbsp;</span></a>");
//			stream << ("<a href=\"configurations.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Configurations&nbsp;&nbsp;&nbsp;</span></a>");
//			stream << ("<a href=\"javascript: $('loading').style.visibility='visible'; window.location='/logout?url=login.html'\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Logout&nbsp;&nbsp;&nbsp;</span></a>");
//		}break;

		case MENU_CONFIGURATIONS:
		{
			stream << ("<a href=\"oldindex.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Home&nbsp;&nbsp;&nbsp;</span></a>");
			stream << ("<a href=\"devices.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Devices&nbsp;&nbsp;&nbsp;</span></a>");
//			stream << ("<a href=\"groups.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Groups&nbsp;&nbsp;&nbsp;</span></a>");
			stream << ("<a href=\"configurations.html\" class=\"mainMenuSelected\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Configurations&nbsp;&nbsp;&nbsp;</span></a>");
			stream << ("<a href=\"javascript: $('loading').style.visibility='visible'; window.location='/logout?url=login.html'\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Logout&nbsp;&nbsp;&nbsp;</span></a>");
		}break;

		default:
		{
			stream << ("<a href=\"oldindex.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Home&nbsp;&nbsp;&nbsp;</span></a>");
			stream << ("<a href=\"devices.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Devices&nbsp;&nbsp;&nbsp;</span></a>");
//			stream << ("<a href=\"groups.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Groups&nbsp;&nbsp;&nbsp;</span></a>");
			stream << ("<a href=\"configurations.html\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Configurations&nbsp;&nbsp;&nbsp;</span></a>");
			stream << ("<a href=\"javascript: $('loading').style.visibility='visible'; window.location='/logout?url=login.html'\" class=\"mainMenu\"><span style=\"position:relative; top:8px\">&nbsp;&nbsp;&nbsp;Logout&nbsp;&nbsp;&nbsp;</span></a>");
		}
	}

	stream << ("</div>");

	result.push_back(divID);
	result.push_back(stream.str());

	return result;
}

std::vector<std::string> SAccount::getHtml(std::string divID, std::string query)
{
	if(query.empty())
		return SAccount::getUserListHtml(divID);
	else
	{
		rgcpp::StringTokenizer tokenizer(query,'#');

		std::string action = tokenizer.nextToken();
		if(action.compare(SC_ACTION_NEW) == 0)
			return SAccount::getNewUserHtml(divID);
		else if(action.compare(SC_ACTION_SHOW) ==  0 && tokenizer.hasMoreElements())
			return SAccount::getUserHtml(divID,tokenizer.nextTokenInt());
		else if(action.compare(SC_ACTION_EDIT) == 0 && tokenizer.hasMoreElements())
			return SAccount::getEditUserHtml(divID,tokenizer.nextTokenInt());
		else
			return SAccount::getUserListHtml(divID);
	}
}

std::vector<std::string> SAccount::createUser(std::string name, int type, std::string login, std::string pass1, std::string pass2)
{
	std::stringstream stream;
	std::vector<std::string> result;

	if(name.empty())
	{
		result.push_back("errorMessage");
		result.push_back("<span class=\"loginMessageError\">Invalid user name!</span>");
		result.push_back("$('userName').style.borderColor = '#CC0505'; $('userType').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userLogin').style.borderColor = '#000000'; $('userPassword1').style.borderColor = '#000000'; $('userPassword2').style.borderColor = '#000000';");
	}
	else if(login.empty())
	{
		result.push_back("errorMessage");
		result.push_back("<span class=\"loginMessageError\">Invalid user login!</span>");
		result.push_back("$('userName').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userLogin').style.borderColor = '#CC0505'; $('userPassword1').style.borderColor = '#000000'; $('userPassword2').style.borderColor = '#000000';");
	}
	else if(pass1.size() < 4)
	{
		result.push_back("errorMessage");
		result.push_back("<span class=\"loginMessageError\">Invalid user password! Password must have at least 4 characters...</span>");
		result.push_back("$('userName').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userLogin').style.borderColor = '#000000'; $('userPassword1').style.borderColor = '#CC0505'; $('userPassword2').style.borderColor = '#CC0505';");
	}
	else if(pass1.compare(pass2))
	{
		result.push_back("errorMessage");
		result.push_back("<span class=\"loginMessageError\">Password and Confirm Password don't match!</span>");
		result.push_back("$('userName').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userLogin').style.borderColor = '#000000'; $('userPassword1').style.borderColor = '#CC0505'; $('userPassword2').style.borderColor = '#CC0505';");
	}
	else if(DataBase::getInstance()->existsUser(login))
	{
		result.push_back("errorMessage");
		result.push_back("<span class=\"loginMessageError\">The user login is already in use! Please choose a different one.!</span>");
		result.push_back("$('userName').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userLogin').style.borderColor = '#CC0505'; $('userPassword1').style.borderColor = '#000000'; $('userPassword2').style.borderColor = '#000000';");
	}
	else
	{
		try
		{
			User user;
			user.setName(name);
			user.setType((UserType)type);
			user.setLogin(login);
			user.setPassword(pass1,false);

			user = DataBase::getInstance()->createUser(user);

			stream << "window.location = '/users.html#show#" << user.getID() << "';";

			result.push_back("errorMessage");
			result.push_back("<span class=\"loginMessageError\"></span>");
			result.push_back(stream.str());
		}
		catch (rgcpp::Exception& e)
		{
			result.push_back("errorMessage");
			result.push_back("<span class=\"loginMessageError\">Server Internal Error! Please check the logs for more information.</span>");
		}
	}

	return result;
}

std::vector<std::string> SAccount::saveUser(int userID, std::string name, int type, std::string login, std::string pass1, std::string pass2)
{
	std::stringstream stream;
	std::vector<std::string> result;

	if(!DataBase::getInstance()->existsUser(userID))
	{
		result.push_back("window.location = '/users.html';");
		return result;
	}

	User user = DataBase::getInstance()->getUser(userID);

	if(name.empty())
	{
		result.push_back("errorMessage");
		result.push_back("<span class=\"loginMessageError\">Invalid user name!</span>");
		result.push_back("$('userName').style.borderColor = '#CC0505'; $('userType').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userLogin').style.borderColor = '#000000'; $('userPassword1').style.borderColor = '#000000'; $('userPassword2').style.borderColor = '#000000';");
	}
	else if(login.empty())
	{
		result.push_back("errorMessage");
		result.push_back("<span class=\"loginMessageError\">Invalid user login!</span>");
		result.push_back("$('userName').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userLogin').style.borderColor = '#CC0505'; $('userPassword1').style.borderColor = '#000000'; $('userPassword2').style.borderColor = '#000000';");
	}
	else if(user.getName().compare(name) != 0 && DataBase::getInstance()->existsUser(login))
	{
		result.push_back("errorMessage");
		result.push_back("<span class=\"loginMessageError\">The user login is already in use! Please choose a different one.!</span>");
		result.push_back("$('userName').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userLogin').style.borderColor = '#CC0505'; $('userPassword1').style.borderColor = '#000000'; $('userPassword2').style.borderColor = '#000000';");
	}
	else if(pass1.size() < 4)
	{
		result.push_back("errorMessage");
		result.push_back("<span class=\"loginMessageError\">Invalid user password! Password must have at least 4 characters...</span>");
		result.push_back("$('userName').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userLogin').style.borderColor = '#000000'; $('userPassword1').style.borderColor = '#CC0505'; $('userPassword2').style.borderColor = '#CC0505';");
	}
	else if(pass1.compare(pass2))
	{
		result.push_back("errorMessage");
		result.push_back("<span class=\"loginMessageError\">Password and Confirm Password don't match!</span>");
		result.push_back("$('userName').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userType').style.borderColor = '#000000'; $('userLogin').style.borderColor = '#000000'; $('userPassword1').style.borderColor = '#CC0505'; $('userPassword2').style.borderColor = '#CC0505';");
	}
	else
	{
		try
		{
			user.setName(name);
			user.setType((UserType)type);
			user.setLogin(login);
			user.setPassword(pass1,false);
			DataBase::getInstance()->updateUser(user);

			stream << "window.location = '/users.html#show#" << user.getID() << "';";

			result.push_back("errorMessage");
			result.push_back("<span class=\"loginMessageError\"></span>");
			result.push_back(stream.str());
		}
		catch (rgcpp::Exception& e)
		{
			result.push_back("errorMessage");
			result.push_back("<span class=\"loginMessageError\">Server Internal Error! Please check the logs for more information.</span>");
		}
	}

	return result;
}

std::vector<std::string> SAccount::removeUser(int userID)
{
	std::vector<std::string> result;
	if(DataBase::getInstance()->getUsers().size() < 2)
	{
		result.push_back(std::string("alert('Unable to delete this user. It is the last user.'); window.location = '/users.html#show#")+rgcpp::ToString(userID)+std::string("';"));
	}
	else
	{
		DataBase::getInstance()->removeUser(userID);
		result.push_back("window.location = '/users.html';");
	}

	return result;
}

std::vector<std::string> SAccount::getUserListHtml(std::string divID)
{
	std::stringstream stream;
	std::vector<std::string> result;

	stream << "<table width=\"1024\">";
	stream << "	<tr><td>";

	stream << "		<br/>";

	stream << "		<div style=\"overflow: hidden; margin-left: 10px;\">";
	stream << "			<span class=\"head1\">Users :: List</span><br/>";
	stream << "		</div>";
	stream << "		<div style=\"overflow: hidden;\">";
	stream << "			<span style=\"float:left; margin-left: 20px; margin-top: 5px;\"><a href=\"/users.html#new\" class=\"textLink\">[new user]</a></span>";
	stream << "		</div>";

	stream << "		<hr/>";

	std::vector<User> users = DataBase::getInstance()->getUsers();

	if(users.empty())
	{
		stream << "	<div style=\"overflow: hidden; margin-left: 20px;\">";
		stream << "<span class=\"text\">No results found...</span>";
		stream << "	</div>";
	}
	else
	{
		  char buffer [80];

		stream << "	<div style=\"overflow: hidden; margin-left: 20px;\">";
		stream << "		<table width=\"1004\">";
		stream << "		<tr class=\"tableRowHeader\">";
		stream << "			<td>Name</td><td>Type</td><td>Login</td><td>Created On</td><td>Updated On</td>";
		stream << "		</tr>";
		for(std::vector<User>::iterator itr=users.begin(); itr!=users.end(); itr++)
		{
			stream << "		<tr cellpadding=\"0\" cellspacing=\"0\" class=\"tableRow\" onmouseover=\"javascript: document.body.style.cursor = 'pointer';\" onmouseout=\"javascript: document.body.style.cursor = 'default';\" onclick=\"javascript: window.location = '/users.html#show#" << itr->getID() << "'; document.body.style.cursor = 'default';\">";
			stream << "			<td style=\"width: 200px;\">" << itr->getName() << "</td><td>" << itr->getTypeStr() << "</td><td>" << itr->getLogin() << "</td><td>" << rgcpp::ToString(itr->getCreation(),"%Y-%m-%d %X") << "</td><td>" << rgcpp::ToString(itr->getUpdated(),"%Y-%m-%d %X") << "</td>";
			stream << "		</tr>";
		}
		stream << "		</table>";
		stream << "	</div>";
	}

	stream << "		<hr/>";

	stream << "	</td></tr>";
	stream << "</table>";

	result.push_back(divID);
	result.push_back(stream.str());

	return result;
}

std::vector<std::string> SAccount::getNewUserHtml(std::string divID)
{
	std::stringstream stream;
	std::vector<std::string> result;

	stream << "<table width=\"1024\">";
	stream << "	<tr><td>";

	stream << "		<br/>";

	stream << "		<div style=\"overflow: hidden; margin-left: 10px;\">";
	stream << "			<span class=\"head1\">Users :: New</span><br/>";
	stream << "		</div>";

	stream << "		<hr/>";

	stream << "<table style=\"margin-left: 20px;\">";
	stream << "	<tr style=\"height: 30px\">";
	stream << "		<td valign=\"top\">";
	stream << "			<span class=\"head3\">Name:&nbsp;&nbsp;&nbsp;</span><br/>";
	stream << "		</td>";
	stream << "		<td>";
	stream << "			<input class=\"inputText\" type=\"text\" id=\"userName\" name=\"userName\" value=\"\" /><br/>";
	stream << "		</td>";
	stream << "	</tr>";
	stream << "	<tr style=\"height: 30px\">";
	stream << "		<td valign=\"top\">";
	stream << "			<span class=\"head3\">Type:&nbsp;&nbsp;&nbsp;</span><br/>";
	stream << "		</td>";
	stream << "		<td>";
	stream << "			<select class=\"inputText\" id=\"userType\" name=\"userType\">";
	stream << "				<option value=\"" << ACS_USER_NORMAL << "\" selected=\"selected\">Normal</option>";
	stream << "				<option value=\"" << ACS_USER_ADMIN << "\" >With Commands</option>";
	stream << "			</select><br/>";
	stream << "		</td>";
	stream << "	</tr>";
	stream << "	<tr style=\"height: 30px\">";
	stream << "		<td valign=\"top\">";
	stream << "			<span class=\"head3\">Login:&nbsp;&nbsp;&nbsp;</span><br/>";
	stream << "		</td>";
	stream << "		<td>";
	stream << "			<input class=\"inputText\" type=\"text\" id=\"userLogin\" name=\"userLogin\" value=\"\" /><br/>";
	stream << "		</td>";
	stream << "	</tr>";
	stream << "	<tr style=\"height: 30px\">";
	stream << "		<td valign=\"top\">";
	stream << "			<span class=\"head3\">Password:&nbsp;&nbsp;&nbsp;</span><br/>";
	stream << "		</td>";
	stream << "		<td>";
	stream << "			<input class=\"inputText\" type=\"password\" id=\"userPassword1\" name=\"userPassword1\" value=\"\" /><br/>";
	stream << "		</td>";
	stream << "	</tr>";
	stream << "	<tr style=\"height: 30px\">";
	stream << "		<td valign=\"top\">";
	stream << "			<span class=\"head3\">Confirm:&nbsp;&nbsp;&nbsp;</span><br/>";
	stream << "		</td>";
	stream << "		<td>";
	stream << "			<input class=\"inputText\" type=\"password\" id=\"userPassword2\" name=\"userPassword2\" value=\"\" /><br/>";
	stream << "		</td>";
	stream << "	</tr>";
	stream << "	<tr style=\"height: 30px\">";
	stream << "		<td></td>";
	stream << "		<td align=\"center\">";
	stream << "			<input type=\"button\" value=\"Create\" style=\"width:60px;\" onclick=\"javascript: $('loading').style.visibility='visible'; SAccount.createUser($('userName').value,$('userType').value,$('userLogin').value,$('userPassword1').value,$('userPassword2').value,redrawLayer)\" />&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"/users.html\" class=\"cancelLink\">Cancel<a>";
	stream << "		</td>";
	stream << "	</tr>";
	stream << "</table>";
	stream << "	<br/>";
	stream << "	<div id=\"errorMessage\"></div></";
	stream << "	<br/>";
	stream << "	</td></tr>";
	stream << "</table>";

	result.push_back(divID);
	result.push_back(stream.str());

	return result;
}

std::vector<std::string> SAccount::getEditUserHtml(std::string divID, int userID)
{
	std::stringstream stream;
	std::vector<std::string> result;

	if(!DataBase::getInstance()->existsUser(userID))
	{
		result.push_back(divID);
		result.push_back(stream.str());
		result.push_back("window.location = '/users.html';");
		return result;
	}

	User user = DataBase::getInstance()->getUser(userID);

	stream << "<table width=\"1024\">";
	stream << "	<tr><td>";

	stream << "		<br/>";

	stream << "		<div style=\"overflow: hidden; margin-left: 10px;\">";
	stream << "			<span class=\"head1\">Users :: Edit</span><br/>";
	stream << "		</div>";

	stream << "		<hr/>";

	stream << "<table style=\"margin-left: 20px;\">";
	stream << "	<tr style=\"height: 30px\">";
	stream << "		<td valign=\"top\">";
	stream << "			<span class=\"head3\">Name:&nbsp;&nbsp;&nbsp;</span><br/>";
	stream << "		</td>";
	stream << "		<td>";
	stream << "			<input class=\"inputText\" type=\"text\" id=\"userName\" name=\"userName\" value=\"" << user.getName() << "\" /><br/>";
	stream << "		</td>";
	stream << "	</tr>";
	stream << "	<tr style=\"height: 30px\">";
	stream << "		<td valign=\"top\">";
	stream << "			<span class=\"head3\">Type:&nbsp;&nbsp;&nbsp;</span><br/>";
	stream << "		</td>";
	stream << "		<td>";
	stream << "			<select class=\"inputText\" id=\"userType\" name=\"userType\">";
	stream << "				<option value=\"" << ACS_USER_NORMAL << "\" " << (user.getType() == ACS_USER_NORMAL? "selected=\"selected\"" : "") << ">Normal</option>";
	stream << "				<option value=\"" << ACS_USER_ADMIN << "\" " << (user.getType() == ACS_USER_ADMIN? "selected=\"selected\"" : "") << ">With Commands</option>";
	stream << "			</select><br/>";
	stream << "		</td>";
	stream << "	</tr>";
	stream << "	<tr style=\"height: 30px\">";
	stream << "		<td valign=\"top\">";
	stream << "			<span class=\"head3\">Login:&nbsp;&nbsp;&nbsp;</span><br/>";
	stream << "		</td>";
	stream << "		<td>";
	stream << "			<input class=\"inputText\" type=\"text\" id=\"userLogin\" name=\"userLogin\" value=\"" << user.getLogin() << "\" /><br/>";
	stream << "		</td>";
	stream << "	</tr>";
	stream << "	<tr style=\"height: 30px\">";
	stream << "		<td valign=\"top\">";
	stream << "			<span class=\"head3\">Password:&nbsp;&nbsp;&nbsp;</span><br/>";
	stream << "		</td>";
	stream << "		<td>";
	stream << "			<input class=\"inputText\" type=\"password\" id=\"userPassword1\" name=\"userPassword1\" value=\"\" /><br/>";
	stream << "		</td>";
	stream << "	</tr>";
	stream << "	<tr style=\"height: 30px\">";
	stream << "		<td valign=\"top\">";
	stream << "			<span class=\"head3\">Confirm:&nbsp;&nbsp;&nbsp;</span><br/>";
	stream << "		</td>";
	stream << "		<td>";
	stream << "			<input class=\"inputText\" type=\"password\" id=\"userPassword2\" name=\"userPassword2\" value=\"\" /><br/>";
	stream << "		</td>";
	stream << "	</tr>";
	stream << "	<tr style=\"height: 30px\">";
	stream << "		<td></td>";
	stream << "		<td align=\"center\">";
	stream << "			<input type=\"button\" value=\"Save\" style=\"width:60px;\" onclick=\"javascript: $('loading').style.visibility='visible'; SAccount.saveUser(" << user.getID() << ",$('userName').value,$('userType').value,$('userLogin').value,$('userPassword1').value,$('userPassword2').value,redrawLayer)\" />&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"/users.html\" class=\"cancelLink\">Cancel<a>";
	stream << "		</td>";
	stream << "	</tr>";
	stream << "</table>";
	stream << "	<br/>";
	stream << "	<div id=\"errorMessage\"></div></";
	stream << "	<br/>";
	stream << "	</td></tr>";
	stream << "</table>";

	result.push_back(divID);
	result.push_back(stream.str());

	return result;
}

std::vector<std::string> SAccount::getUserHtml(std::string divID, int userID)
{
	std::stringstream stream;
	std::vector<std::string> result;

	if(!DataBase::getInstance()->existsUser(userID))
	{
		result.push_back(divID);
		result.push_back(stream.str());
		result.push_back("window.location = '/users.html';");
		return result;
	}

	User user = DataBase::getInstance()->getUser(userID);

	stream << "<table width=\"1024\">";
	stream << "	<tr><td>";

	stream << "		<br/>";

	stream << "		<div style=\"overflow: hidden; margin-left: 10px;\">";
	stream << "			<span class=\"head1\">Users :: " << user.getName() << "</span><br/>";
	stream << "			<span style=\"float:left; margin-left: 10px; margin-top: 5px;\"><a href=\"/users.html#edit#" << user.getID() << "\" class=\"textLink\">[edit]</a></span>&nbsp;&nbsp;";
	stream << "			<span style=\"float:left; margin-left: 10px; margin-top: 5px;\"><a href=\"javascript: if(confirm('Are you sure you want to delete this user?')){$('loading').style.visibility='visible'; SAccount.removeUser(" << userID << ",executeScriptLoading);}\" class=\"textLink\">[delete]</a></span>";
	stream << "		</div>";

	stream << "		<hr/>";

	stream << "		<div style=\"margin-left: 20px;\">";
	stream << "			<span class=\"head3\">Name:&nbsp;</span><span class=\"text\">" << user.getName() << "</span><br/>";
	stream << "			<span class=\"head3\">Login:&nbsp;</span><span class=\"text\">" << user.getLogin() << "</span><br/>";
	stream << "			<span class=\"head3\">Type:&nbsp;</span><span class=\"text\">" << user.getTypeStr() << "</span><br/>";
	stream << "			<span class=\"head3\">Created on:&nbsp;</span><span class=\"text\">" << rgcpp::ToString(user.getCreation(),"%Y-%m-%d %X") << "</span><br/>";
	stream << "			<span class=\"head3\">Last update on:&nbsp;</span><span class=\"text\">" << rgcpp::ToString(user.getUpdated(),"%Y-%m-%d %X") << "</span><br/>";
	stream << "		</div>";

	stream << "	</td></tr>";
	stream << "</table>";

	result.push_back(divID);
	result.push_back(stream.str());

	return result;
}

}
